﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.Finance.Basedatas.Repository;
using mozhi.smarterp.Finance.EntityFrameworkCore;
using mozhi.smarterp.Finance.Querys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.Finance.Basedatas
{
    public class PrintTemplateRepository : EfCoreRepository<FinanceDbContext, PrintTemplate, Guid>, IPrintTemplateRepository
    {
        public PrintTemplateRepository(IDbContextProvider<FinanceDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<List<PrintTemplate>> FindAsync(PrintTemplatePageQuery query)
        {
            var printTemplates = await DbContext.Set<PrintTemplate>().AsNoTracking()
                .WhereIf(!query.Keyword.IsNullOrEmpty(), x => x.Name.Contains(query.Keyword))
                .WhereIf(query.Ids != null && query.Ids.Count > 0, x => query.Ids.Contains(x.Id))
                .WhereIf(query.Activated.HasValue, x => x.Activated == query.Activated.Value)
                .WhereIf(query.ExclusionId.HasValue, x => x.Id != query.ExclusionId.Value)
                .WhereIf(!query.ModuleCode.IsNullOrEmpty(), x => x.ModuleCode == query.ModuleCode)
                .Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize)
                .ToListAsync();

            return printTemplates;
        }

        public async Task<int> GetCountAsync(PrintTemplatePageQuery query)
        {
            var count = await DbContext.Set<PrintTemplate>().AsNoTracking()
              .WhereIf(!query.Keyword.IsNullOrEmpty(), x => x.Name.Contains(query.Keyword))
              .WhereIf(!query.Name.IsNullOrEmpty(), x => x.Name == query.Name)
              .WhereIf(query.Ids != null && query.Ids.Count > 0, x => query.Ids.Contains(x.Id))
              .WhereIf(query.Activated.HasValue, x => x.Activated == query.Activated.Value)
              .WhereIf(query.ExclusionId.HasValue, x => x.Id != query.ExclusionId.Value)
              .WhereIf(!query.ModuleCode.IsNullOrEmpty(), x => x.ModuleCode == query.ModuleCode)
              .CountAsync();

            return count;
        }
    }
}
